package com.gy.smart.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.reflect.TypeToken;
import com.gy.smart.R;
import com.gy.smart.base.BaseBean;
import com.gy.smart.base.BaseFragment;
import com.gy.smart.base.WebViewActivity;
import com.gy.smart.home.adapter.DynamicAdapter;
import com.gy.smart.home.bean.DynamicListBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.StringConstants;
import com.gy.smart.util.Urls;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2017/1/4.
 * Describe:昌平动态
 */
@SuppressLint("ValidFragment")
public class DynamicFragment extends BaseFragment {

    private String flag;
    private PullToRefreshListView listView;
    private DynamicAdapter adapter;
    private List<DynamicListBean> mDatas,requestList;
    private int page = 1;

    public DynamicFragment(){}

    public DynamicFragment(String flag){
        this.flag = flag;
    }


    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_dynamic,null);
        hidenTop();
        return view;
    }

    @Override
    protected void initView() {
        listView = (PullToRefreshListView) _rootView.findViewById(R.id.fg_dynamic_list);
    }

    @Override
    protected void initData() {
        listView.setMode(PullToRefreshBase.Mode.DISABLED);
        mDatas = new ArrayList<>();
        requestList = new ArrayList<>();
        adapter = new DynamicAdapter(mContext,mDatas);
    }

    @Override
    protected void setListener() {
        super.setListener();
        listView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                if(!isLoading && requestList.size() == StringConstants.PAGENUM){
                    isLoading = true;
                    page++;
                    getList();
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(WebViewActivity.TITLE,"昌平动态");
                intent.putExtra(WebViewActivity.URL,Urls.web_url+mDatas.get(position - 1).id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(getActivity());
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        showLoadDialog();
        getList();
    }

    /** 获取列表*/
    private void getList(){
        RequestParams params = new RequestParams(Urls.url_news_list);
        params.addParameter("cat_id",flag);
        params.addParameter("page",page+"");
        params.addParameter("pagenum", StringConstants.PAGENUM+"");
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    Type type = new TypeToken<List<DynamicListBean>>(){}.getType();
                    requestList = gson.fromJson(result.data,type);
                    mDatas.addAll(requestList);
                    adapter.notifyDataSetChanged();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
